package com.geektime.congestion.auth.domain;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.HashMap;
import java.util.Map;

@NoArgsConstructor
@Getter
@ToString
public class ApiRequest {

    private String baseurl;

    private String token;

    private String appId;

    private long timestamp;

    public ApiRequest(String baseurl, String token, String appId, long timestamp) {
        this.baseurl = baseurl;
        this.token = token;
        this.appId = appId;
        this.timestamp = timestamp;
    }

    // /user?token=xxx&appId=xxx&timestamp=xxx
    public static ApiRequest createFromFullUrl(String url) {
        String[] split = url.split("\\?");
        String baseurl = split[0];
        Map<String, String> querys = new HashMap<>();
        String[] params = split[1].split("&");
        for (int i = 0; i < params.length; i++) {
            String[] strings = params[i].split("=");
            querys.put(strings[0], strings[1]);
        }
        return new ApiRequest(baseurl, querys.get("token"), querys.get("appId"), Long.parseLong(querys.get("timestamp")));
    }


}
